<?php

namespace Common\Logic;
class ImageLogic{
	//************************************图片上传******************************************
	/**
	 * base64位
	 * @param unknown $image
	 */
	public function uploadImage($images){
		header('Access-Control-Allow-Origin: *');
		$image = new \Think\Image();
		$base64_image = $images;
		//匹配出图片的格式
		if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image, $result)){
			$type = $result[2];
			$file = "./Uploads/";
			if(!file_exists($file))
			{
				//检查是否有该文件夹，如果没有就创建，并给予最高权限
				mkdir($file, 0777);
			}
			$rand = md5(rand(1000, 9999));
			$new_file = $file.$rand.".".$type;
			if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image)))){
				$image->open($new_file);
				$new_file1 = $file.$rand.".".$type;
				$r= $image->thumb(2000, 2000)->save($new_file1);//压缩图片	
				$return['url'] = $new_file1;
				$return['status'] = 1;
				$return['info'] = "上传成功";
				return $return;
			}else{
				$return['status'] = -1;
				$return['info'] = "上传失败";
				return $return;
			}
		}else{
			return ['status'=>-1,'msg'=>'参数错误，数据格式不正确'];
		}
	}
	/**
	 * post formdata 上传
	 */
	/**
	 * 单个上传图片
	 */
	public function uploadPic(){
		$config = array(
				'maxSize'       =>  0, //上传的文件大小限制 (0-不做限制)
				'exts'          =>  array('jpg','png','gif','jpeg'), //允许上传的文件后缀
				'rootPath'      =>  './Upload/', //保存根路径
				'driver'        =>  'LOCAL', // 文件上传驱动
				'subName'       =>  array('date', 'Y-m'),
				'savePath'      =>  I('dir','uploads')."/"
		);
		$dirs = explode(",",C("WST_UPLOAD_DIR"));
		if(!in_array(I('dir','uploads'), $dirs)){
			echo '非法文件目录！';
			return false;
		}
	
		$upload = new \Think\Upload($config);
		$rs = $upload->upload($_FILES);
		$Filedata = key($_FILES);
		if(!$rs){
			$this->error($upload->getError());
		}else{
			$images = new \Think\Image();
			$images->open('./Upload/'.$rs[$Filedata]['savepath'].$rs[$Filedata]['savename']);
			$newsavename = str_replace('.','_thumb.',$rs[$Filedata]['savename']);
			$vv = $images->thumb(I('width',300), I('height',300))->save('./Upload/'.$rs[$Filedata]['savepath'].$newsavename);
			if(C('WST_M_IMG_SUFFIX')!=''){
				$msuffix = C('WST_M_IMG_SUFFIX');
				$mnewsavename = str_replace('.',$msuffix.'.',$rs[$Filedata]['savename']);
				$mnewsavename_thmb = str_replace('.',"_thumb".$msuffix.'.',$rs[$Filedata]['savename']);
				$images->open('./Upload/'.$rs[$Filedata]['savepath'].$rs[$Filedata]['savename']);
				$images->thumb(I('width',700), I('height',700))->save('./Upload/'.$rs[$Filedata]['savepath'].$mnewsavename);
				$images->thumb(I('width',250), I('height',250))->save('./Upload/'.$rs[$Filedata]['savepath'].$mnewsavename_thmb);
			}
			header("Content-type: text/html; charset=utf-8");
			$sss = vendor('aliyun.autoload');
			$ossClient = new \OSS\OssClient(C('accessKeyId'), C('accessKeySecret'), C('endpoint'));
			$object = date('Y-m-d') . '/' . $rs[$Filedata]['savename']; // 想要保存文件的名称
			$file = "Upload/".$rs[$Filedata]['savepath'].$rs[$Filedata]['savename']; // 文件路径，必须是本地的。
			// 			dump($file);die;
			try {
				$getOssInfo = $ossClient->uploadFile(C('bucket'), $object, $file);
				$getOssPdfUrl = $getOssInfo['info']['url'];
				// 				dump($getOssPdfUrl);
				if ($getOssPdfUrl) {
					unlink($file);
					$this->ajaxReturn(['status'=>1,'url'=>$getOssPdfUrl]);
				}
			} catch (OssException $e) {
				$this->ajaxReturn(['status'=>-1,'msg'=>$e->getMessage() . "\n"]);
				return;
			}
		}
	}
	
	
	/**
	 * base64位
	 * @param unknown $image
	 */
	public function uploadImageBendi($images){
		header('Access-Control-Allow-Origin: *');
		$image = new \Think\Image();
		$base64_image = $images;
		//匹配出图片的格式
		if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image, $result)){
			$type = $result[2];
			$file = "./Uploads/";
			if(!file_exists($file))
			{
				//检查是否有该文件夹，如果没有就创建，并给予最高权限
				mkdir($file, 0777);
			}
			$rand = md5(rand(1000, 9999));
			$new_file = $file.$rand.".".$type;
			if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image)))){
				$image->open($new_file);
				$new_file1 = $file.$rand.".".$type;
				$r= $image->thumb(200, 200)->save($new_file1);//压缩图片
				$return['status'] = 1;
				$return['info'] = "上传成功";
				$return['url'] = $new_file1;
			}else{
				$return['status'] = -1;
				$return['info'] = "上传失败功";
			}
		}else{
			return ['status'=>-1,'msg'=>'参数错误，数据格式不正确'];
		}
	}
}